package com.zhengke.controller;


import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.zhengke.common.util.object.AjaxDone;
import com.zhengke.pojo.SysDictionaryItem;
import com.zhengke.pojo.TvUserIntegralLog;
import com.zhengke.pojo.archie.CurrentUser;
import com.zhengke.pojo.archie.OrderPagination;
import com.zhengke.pojo.archie.TvUser;
import com.zhengke.service.AccountCenterService;
 

@Controller
public class AccountCenterController extends OrderPagination {
	@Autowired
	AccountCenterService service;
	
	@RequestMapping("toAccountCenter.do")
	public String toAccountCenter(HttpServletRequest request, ModelMap model) {
		
		return "tv/accountCenter/accountCenter";
	}
	
	/**
	 * 去个人中心页面
	 */
	@RequestMapping("toUserCenter.do")
	public String toUserCenter(HttpServletResponse response,HttpServletRequest request,ModelMap model) {
		CurrentUser user =	getCurrentUser(request, response);
		String user_id =	user.getTvnNo();
		TvUser tvUser = service.queryTvUser(user_id);
		model.put("tmp", new Date());
		model.put("userInte", tvUser.getUser_integral());
		model.put("userBalan", tvUser.getUser_balance());
		model.put("tvnNo", user_id);
		return "tv/accountCenter/userCenter";
		
	}
	/**
	 * 个人中心详情
	 */
	@RequestMapping("userData.do")
	public void userData(HttpServletRequest request,HttpServletResponse response) {
		CurrentUser user =	getCurrentUser(request, response);
		String tvnNo =	user.getTvnNo();
		try{
			List<TvUserIntegralLog> list = service.queryIntegerList(tvnNo);
			List<Map<String,String>> rtnList = new ArrayList<Map<String,String>>();
			for(TvUserIntegralLog to : list){
				Map<String,String> temp = new HashMap<String,String>();
				temp.put("inteNum", to.getIntegralNum().toString());
				temp.put("inteTime", to.getTime());
				if(to.getSign()==0){
					temp.put("sign", "抵扣");
				}
				if(to.getSign()==1){
					temp.put("sign", "赠送");
				}
				rtnList.add(temp);
			}
			response.setCharacterEncoding("utf-8");
			response.setHeader("Pragma","No-cache"); 
			response.setHeader("Cache-Control","no-cache"); 
			response.setDateHeader("Expires", 0);  
			response.getWriter().print("var dataList="+JSONArray.parseArray(JSON.toJSONString(rtnList)));	
		}catch(Exception e){
			e.printStackTrace();
		}	
	}
	/**
	 * 去送货信息界面
	 * @param request
	 * @param response
	 */
	@RequestMapping("toDeliveryTime.do")
	public String toDeliveryTime(HttpServletResponse response,HttpServletRequest request,ModelMap model) {
		CurrentUser user =	getCurrentUser(request, response);
		String user_id =	user.getTvnNo();
		model.put("tvnNo", user_id);
		model.put("tmp", new Date());
		return "tv/accountCenter/deliveryTime";
	}
	/**
	 * 查询送货时间
	 */
	@RequestMapping("deliveryTime.do")
	public void deliveryTime(HttpServletResponse response) {
	try {
		List<SysDictionaryItem> sysDicItem= service.queryDicItem();
		List<Map<String,String>> rtnList = new ArrayList<Map<String,String>>();
		Map<String,String> map = new HashMap<String,String>();
		map.put("deployId", "");
		map.put("deployInfo", "自提");
		rtnList.add(map);
		for(SysDictionaryItem to : sysDicItem){
			Map<String,String> temp = new HashMap<String,String>();
			temp.put("deployId", to.getItemId());
			temp.put("deployInfo", to.getParamValue());
			rtnList.add(temp);
		}
		response.setCharacterEncoding("utf-8");
		response.setHeader("Pragma","No-cache"); 
		response.setHeader("Cache-Control","no-cache"); 
		response.setDateHeader("Expires", 0);  
		response.getWriter().print("var dataList="+JSONArray.parseArray(JSON.toJSONString(rtnList)));
		} catch (IOException e) {
			e.printStackTrace();
		}	
	}
	@ResponseBody
	@RequestMapping("setDeliveryTime.do")
	public AjaxDone setDeliveryTime(HttpServletResponse response,HttpServletRequest request) {
		CurrentUser user =	getCurrentUser(request, response);
		String user_id =	user.getTvnNo();
		String deployId =(String) request.getAttribute("deployId");
		String deployInfo = (String) request.getAttribute("deployInfo");
		try{
			TvUser tvUser = service.queryTvUser(user_id);
			if(deployId==null||"".equals(deployId)){
				tvUser.setDelivery_way("1");
				tvUser.setReceipt_time("");
				user.setDeliveryWay("1");
			}
			else{
				tvUser.setDelivery_way("0");
				tvUser.setReceipt_time(deployInfo);
				user.setDeliveryWay("1");
			}
			service.updTime(tvUser);
			user.setReceiptTime(deployInfo);
		
			HttpSession session = request.getSession();
			session.setAttribute("SESSION_USER_KEY", user);
		}catch (Exception e) {
			e.printStackTrace();
		}
		return new AjaxDone("200", "设置成功", "");
	}	
}
